package com.example.choupaiapp.main.activity.mine;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.zzhoujay.richtext.RichText;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import butterknife.BindView;

/**
 * Created by ASUS on 2019/10/29.
 */

public class AboutRichTextActivity extends CommonActivity {

    @BindView(R.id.textview)
    TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agrement);
    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String flag = bundle.getString("flag");
            if (flag.equals("1")) {
                setTitle("法律声明");
                String str = readAssertResource(context,"legal_notice.txt");
                RichText.fromHtml(str)
                        .bind(this)
                        .into(textview);
            } else if (flag.equals("2")) {
                setTitle("使用帮助");
                String str = readAssertResource(context,"use_help.txt");
                RichText.fromHtml(str)
                        .bind(this)
                        .into(textview);
            } else if (flag.equals("3")) {
                setTitle("隐私政策");
                String str = readAssertResource(context,"secret.txt");
                RichText.fromHtml(str)
                        .bind(this)
                        .into(textview);
            } else if (flag.equals("4")) {
                setTitle("承诺书");
                String str = readAssertResource(context,"pledge.txt");
                RichText.fromHtml(str)
                        .bind(this)
                        .into(textview);
            }

        }


    }

    public static String readAssertResource(Context context, String strAssertFileName) {
        AssetManager assetManager = context.getAssets();
        String strResponse = "";
        try {
            InputStream ims = assetManager.open(strAssertFileName);
            strResponse = getStringFromInputStream(ims);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strResponse;
    }

    private static String getStringFromInputStream(InputStream a_is) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(a_is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }
        }
        return sb.toString();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.clear(this);
    }

}
